"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : tools
Case Name   : 使用gaussdb启动数据库指定-O启动后修改系统表结构是否成功
Create At   : 2021/1/18
Owner       : opentestcase003
Description :
    1.关闭正在运行的数据库
    gs_ctl stop -D /opt/openGauss_luz/cluster/dn1
    2.使用gaussdb工具指定-O启动数据库
    gaussdb  -D /opt/openGauss_zl/cluster/dn1 -p 19701 -O &
    3.更改系统表pg_am的表结构
Expect      :
    1.关闭正在运行的数据库成功
    2.使用gaussdb工具制动-O启动数据库成功
    3.更改表无权限
History     :
    modeified by opentestcase042 at 2022-7-12:优化用例，增加等待时间
"""
import os
import unittest
import time
from yat.test import Node
from yat.test import macro
from testcase.utils.ComThread import ComThread
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class Tools(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.userNode = Node(node='PrimaryDbUser')
        self.userNode2 = Node(node='PrimaryDbUser')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.common = Common()

    def test_systools(self):
        self.logger.info('--------关闭正在运行的数据库--------')
        excute_cmd1 = f'source {self.DB_ENV_PATH};gs_ctl stop -D ' \
                      f'{self.DB_INSTANCE_PATH}'
        self.logger.info(excute_cmd1)
        msg1 = self.userNode.sh(excute_cmd1).result()
        self.logger.info(msg1)
        self.logger.info('-------使用gaussdb工具后台运行进程--------')
        excute_cmd3 = f'source {self.DB_ENV_PATH};' \
                      f'gaussdb -D {self.DB_INSTANCE_PATH} ' \
                      f'-p {self.userNode.db_port} -O'
        self.logger.info(excute_cmd3)
        thread_2 = ComThread(self.userNode2.sh, args=(excute_cmd3,))
        thread_2.setDaemon(True)
        thread_2.start()
        thread_2.join(10)
        time.sleep(15)
        self.logger.info('------------更改系统表pg_am的表结构-----------')
        sql_cmd5 = 'alter table pg_am add(cluster int);'
        msg5 = self.sh_primy.execut_db_sql(sql_cmd5)
        self.logger.info(msg5)
        self.assertTrue(msg5.find('permission denied') > -1)
        self.logger.info('----------查看环境是否主备，主备需恢复状态---------')
        check_status_cmd = f'source {self.DB_ENV_PATH};gs_om -t status ' \
                           f'--detail'
        check_status_msg = self.userNode.sh(check_status_cmd).result()
        self.logger.info(check_status_msg)
        if 'Standby' in check_status_msg:
            self.logger.info('------------恢复主备集群状态-----------')
            excute_cmd6 = f'source {self.DB_ENV_PATH};' \
                          f'gs_ctl restart -D \'{self.DB_INSTANCE_PATH}\' ' \
                          f'-M primary'
            msg6 = self.userNode.sh(excute_cmd6).result()
            self.logger.info(msg6)
        else:
            self.logger.info('单机环境不需要恢复')
        time.sleep(15)
        self.logger.info('-----------检查集群状态是否正常----------')
        excute_cmd7 = f'source {self.DB_ENV_PATH};gs_om -t status --detail'
        msg7 = self.userNode.sh(excute_cmd7).result()
        self.logger.info(msg7)
        if 'Standby' in msg7:
            self.logger.info('-----------检查主备状态正常----------')
            self.assertTrue(
                'P Primary Normal' in msg7 and 'S Standby Normal' in msg7)
        else:
            self.logger.info('-----------检查单机状态正常----------')
            self.assertTrue('P Primary Normal' in msg7)

    def tearDown(self):
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
